﻿using System.Linq;
using System;
using DevExpress.Web.ASPxEditors;
using DevExpress.Web.ASPxGridView;
using System.Web.UI.WebControls;
using DevExpress.Web.ASPxTreeView;
public partial class Demo_EditDemo : Frame.Pages.ActionInfomationEditBasePage
{
    protected override string ActionID
    {
        get { return "0"; }
    }

    protected override string UserFunctions
    {
        get
        {
            return "IUDQ";
        }
    }

    protected override bool IsShowSpecialFunctions
    {
        get
        {
            return false;
        }
    }

    protected override void Page_Load(object sender, EventArgs e)
    {
        //显示条件编辑框
        //gridView.ShowFilterControl();
        //gridView.SettingsText.FilterBarClear = "clear";
    }

    protected void gridView_HtmlEditFormCreated(object sender, DevExpress.Web.ASPxGridView.ASPxGridViewEditFormEventArgs e)
    {
    }
    protected void gridView_CellEditorInitialize(object sender, ASPxGridViewEditorEventArgs e)
    {
        //if (gridView.SettingsText.PopupEditFormCaption == "详细")
        //{
        //    //当为查看时，所以列不可编辑(by SU)
        //    e.Editor.Enabled = false;
        //}
        if (e.Column.FieldName == "bmbm")
        {
            //if (e.Value != null)
            //{
            //    //(e.Editor as ASPxDropDownEdit).KeyValue = e.Value;
            //}
            //else
            //{
                using (DAL.DataClassesDataContext dc = new DAL.DataClassesDataContext())
                {
                    ASPxTreeView tree = (e.Editor as ASPxDropDownEdit).FindControl("Tree") as ASPxTreeView;

                    var pnodes = dc.css_department.Where(d => d.lid == 1);
                    foreach (var item in pnodes)
                    {
                        TreeViewNode pnode = new TreeViewNode(item.bmmc, item.bmbm, "../Images/icon/Depatment.png");
                        CreatChildTree(pnode);
                        tree.Nodes.Add(pnode);
                    }
                    tree.ExpandAll();
                }
            //}
        }
    }

    void CreatChildTree(TreeViewNode pnode)
    {
        using (DAL.DataClassesDataContext dc = new DAL.DataClassesDataContext())
        {
            var snodes = dc.css_department.Where(sd => sd.fbmbm == pnode.Name);
            if (snodes.Count() > 0)
            {
                foreach (var sitem in snodes)
                {
                    TreeViewNode snode = new TreeViewNode(sitem.bmmc, sitem.bmbm, "../Images/icon/Door.png");
                    var ssnodes = dc.css_department.Where(ssd => ssd.fbmbm == sitem.bmbm);
                    //递归实现无线构造
                    CreatChildTree(snode);
                    pnode.Nodes.Add(snode);
                }
            }
        }
    }
}